//sound implementation
var sound = Components.classes["@mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
var IOService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var localFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);

// Make a namespace.
if (typeof Samplerbox == 'undefined') {
  var Samplerbox = {};
}

/**
 * Controller for pane.xul
 */
Samplerbox.PaneController = {

  /**
   * Called when the pane is instantiated
   */
  onLoad: function() {
    this._initialized = true;
    this._strings = document.getElementById("samplerbox-strings");
    
    // Make a local variable for this controller so that
    // it is easy to access from closures.
    var controller = this;
                           
sound.init();         
  },
  
  onBrowseFile: function(soundnumber) {
    const nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    fp.init(window, this._strings.getString("ChooseSoundFile"), nsIFilePicker.modeOpen);    
    if (navigator.platform == "MacPPC" || 
        navigator.platform == "MacIntel") {
      fp.appendFilter(this._strings.getString("SoundFileFilter") + " (*.wav, *.aiff)" , "*.wav; *.aiff");
    }
    else {
      fp.appendFilter(this._strings.getString("SoundFileFilter") + " (*.wav)", "*.wav");
    }

    var ret = fp.show();
    if (ret == nsIFilePicker.returnOK || ret == nsIFilePicker.returnReplace) {
      var file = fp.file;
      var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
      pref.setCharPref("extensions.samplerbox.sound" + soundnumber, file.path);
      pref.setCharPref("extensions.samplerbox.name" + soundnumber, file.leafName);
      return document.getElementById("sound-file" + soundnumber).value = file.leafName;
    }
  },
  
  preset_drumset: function() {
  var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
  var custompref = pref.getCharPref("extensions.samplerbox.currentpreset");
  
  if (custompref != "drumsset") {
  pref.setCharPref("extensions.samplerbox.currentpreset","drumsset");
  var customhbox = document.getElementById("custom");
  while (customhbox.firstChild) customhbox.removeChild(customhbox.firstChild);
  }
  
  if (navigator.platform == "Win32") {
  var slash = "\\"; }
  else {
  var slash = "/"; }
  var file = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager).getInstallLocation('sounds@geekshadow.com').getItemLocation('sounds@geekshadow.com'); 
      pref.setCharPref("extensions.samplerbox.sound1", file.path + slash + 'sounds' + slash + 'drums' + slash + 'kick.wav');
      pref.setCharPref("extensions.samplerbox.sound2", file.path + slash + 'sounds' + slash + 'drums' + slash + 'snare.wav');
      pref.setCharPref("extensions.samplerbox.sound3", file.path + slash + 'sounds' + slash + 'drums' + slash + 'high_hat.wav');
      pref.setCharPref("extensions.samplerbox.sound4", file.path + slash + 'sounds' + slash + 'drums' + slash + 'high_tom.wav');
      pref.setCharPref("extensions.samplerbox.sound5", file.path + slash + 'sounds' + slash + 'drums' + slash + 'crash.wav');
      pref.setCharPref("extensions.samplerbox.sound6", file.path + slash + 'sounds' + slash + 'drums' + slash + 'bell.wav');
      pref.setCharPref("extensions.samplerbox.sound7", file.path + slash + 'sounds' + slash + 'drums' + slash + 'claves.wav');
      pref.setCharPref("extensions.samplerbox.sound8", file.path + slash + 'sounds' + slash + 'drums' + slash + 'timbale.wav');
      pref.setCharPref("extensions.samplerbox.sound9", file.path + slash + 'sounds' + slash + 'drums' + slash + 'kick2.wav');
      pref.setCharPref("extensions.samplerbox.sound10", file.path + slash + 'sounds' + slash + 'drums' + slash + 'snare2.wav');
      pref.setCharPref("extensions.samplerbox.sound11", file.path + slash + 'sounds' + slash + 'drums' + slash + 'crash2.wav');
      pref.setCharPref("extensions.samplerbox.sound12", file.path + slash + 'sounds' + slash + 'drums' + slash + 'low_tom.wav');
      pref.setCharPref("extensions.samplerbox.sound13", "");
      pref.setCharPref("extensions.samplerbox.sound14", "");
      pref.setCharPref("extensions.samplerbox.sound15", "");
      pref.setCharPref("extensions.samplerbox.sound16", "");
      pref.setCharPref("extensions.samplerbox.name1", this._strings.getString("kick")+" 1");
      pref.setCharPref("extensions.samplerbox.name2", this._strings.getString("snare")+" 1");
      pref.setCharPref("extensions.samplerbox.name3", this._strings.getString("hihat"));
      pref.setCharPref("extensions.samplerbox.name4", this._strings.getString("hightom"));
      pref.setCharPref("extensions.samplerbox.name5", this._strings.getString("crashcymbal")+" 1");
      pref.setCharPref("extensions.samplerbox.name6", this._strings.getString("bell"));
      pref.setCharPref("extensions.samplerbox.name7", this._strings.getString("claves"));
      pref.setCharPref("extensions.samplerbox.name8", this._strings.getString("timbale"));
      pref.setCharPref("extensions.samplerbox.name9", this._strings.getString("kick")+" 2");
      pref.setCharPref("extensions.samplerbox.name10", this._strings.getString("snare")+" 2");
      pref.setCharPref("extensions.samplerbox.name11", this._strings.getString("crashcymbal")+" 2");
      pref.setCharPref("extensions.samplerbox.name12", this._strings.getString("lowtom"));
      pref.setCharPref("extensions.samplerbox.name13", "");
      pref.setCharPref("extensions.samplerbox.name14", "");
      pref.setCharPref("extensions.samplerbox.name15", "");
      pref.setCharPref("extensions.samplerbox.name16", "");      
      return document.getElementById("sound-file1").value = this._strings.getString("kick")+" 1";
      return document.getElementById("sound-file2").value = this._strings.getString("snare")+" 1";
      return document.getElementById("sound-file3").value = this._strings.getString("hihat");
      return document.getElementById("sound-file4").value = this._strings.getString("hightom");
      return document.getElementById("sound-file5").value = this._strings.getString("crashcymbal")+" 1";
      return document.getElementById("sound-file6").value = this._strings.getString("bell");
      return document.getElementById("sound-file7").value = this._strings.getString("claves");
      return document.getElementById("sound-file8").value = this._strings.getString("timbale");
      return document.getElementById("sound-file9").value = this._strings.getString("kick")+" 2";
      return document.getElementById("sound-file10").value = this._strings.getString("snare")+" 2";
      return document.getElementById("sound-file11").value = this._strings.getString("crashcymbal")+" 2";
      return document.getElementById("sound-file12").value = this._strings.getString("lowtom");
      return document.getElementById("sound-file13").value = "";
      return document.getElementById("sound-file14").value = "";
      return document.getElementById("sound-file15").value = "";
      return document.getElementById("sound-file16").value = "";      
			},
			
  preset_none: function() {
  var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
  var custompref = pref.getCharPref("extensions.samplerbox.currentpreset");
  
  if (custompref != "none") {
  pref.setCharPref("extensions.samplerbox.currentpreset","none");
  var customhbox = document.getElementById("custom");
  while (customhbox.firstChild) customhbox.removeChild(customhbox.firstChild);
  }
  
//  var file = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager).getInstallLocation('SamplerBox@geekshadow.com').getItemLocation('SamplerBox@geekshadow.com'); 
      pref.setCharPref("extensions.samplerbox.sound1", "");
      pref.setCharPref("extensions.samplerbox.sound2", "");
      pref.setCharPref("extensions.samplerbox.sound3", "");
      pref.setCharPref("extensions.samplerbox.sound4", "");
      pref.setCharPref("extensions.samplerbox.sound5", "");
      pref.setCharPref("extensions.samplerbox.sound6", "");
      pref.setCharPref("extensions.samplerbox.sound7", "");
      pref.setCharPref("extensions.samplerbox.sound8", "");
      pref.setCharPref("extensions.samplerbox.sound9", "");
      pref.setCharPref("extensions.samplerbox.sound10", "");
      pref.setCharPref("extensions.samplerbox.sound11", "");
      pref.setCharPref("extensions.samplerbox.sound12", "");
      pref.setCharPref("extensions.samplerbox.sound13", "");
      pref.setCharPref("extensions.samplerbox.sound14", "");
      pref.setCharPref("extensions.samplerbox.sound15", "");
      pref.setCharPref("extensions.samplerbox.sound16", "");
      pref.setCharPref("extensions.samplerbox.name1", "");
      pref.setCharPref("extensions.samplerbox.name2", "");
      pref.setCharPref("extensions.samplerbox.name3", "");
      pref.setCharPref("extensions.samplerbox.name4", "");
      pref.setCharPref("extensions.samplerbox.name5", "");
      pref.setCharPref("extensions.samplerbox.name6", "");
      pref.setCharPref("extensions.samplerbox.name7", "");
      pref.setCharPref("extensions.samplerbox.name8", "");
      pref.setCharPref("extensions.samplerbox.name9", "");
      pref.setCharPref("extensions.samplerbox.name10", "");
      pref.setCharPref("extensions.samplerbox.name11", "");
      pref.setCharPref("extensions.samplerbox.name12", "");
      pref.setCharPref("extensions.samplerbox.name13", "");
      pref.setCharPref("extensions.samplerbox.name14", "");
      pref.setCharPref("extensions.samplerbox.name15", "");
      pref.setCharPref("extensions.samplerbox.name16", "");
      return document.getElementById("sound-file1").value = "";
      return document.getElementById("sound-file2").value = "";
      return document.getElementById("sound-file3").value = "";
      return document.getElementById("sound-file4").value = "";
      return document.getElementById("sound-file5").value = "";
      return document.getElementById("sound-file6").value = "";
      return document.getElementById("sound-file7").value = "";
      return document.getElementById("sound-file8").value = "";
      return document.getElementById("sound-file9").value = "";
      return document.getElementById("sound-file10").value = "";
      return document.getElementById("sound-file11").value = "";
      return document.getElementById("sound-file12").value = "";
      return document.getElementById("sound-file13").value = "";
      return document.getElementById("sound-file14").value = "";
      return document.getElementById("sound-file15").value = "";
      return document.getElementById("sound-file16").value = "";
			},
			
play: function(soundnumber) {
         var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
         var url = pref.getCharPref("extensions.samplerbox.sound" + soundnumber);
         if (url == "") {
         alert(this._strings.getString("nosound"));
         }
         else {
         localFile.initWithPath(url);
         sound.play(IOService.newFileURI(localFile)); } },
         



  /**
   * Called when the pane is about to close
   */
  onUnLoad: function() {
    this._initialized = false;
  },
 
};

window.addEventListener("load", function(e) { Samplerbox.PaneController.onLoad(e); }, false);
window.addEventListener("unload", function(e) { Samplerbox.PaneController.onUnLoad(e); }, false);